package com.forum.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.forum.common.api.CommonResult;
import com.forum.model.dto.CommentDTO;
import com.forum.model.vo.CommentVO;
import com.forum.service.CommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "评论管理接口")
@RestController
@RequestMapping("/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;
    private final ObjectMapper objectMapper;

    @Operation(summary = "创建评论")
    @PostMapping("/create")
    public CommonResult<CommentVO> createComment(@Validated @RequestBody CommentDTO commentDTO) {
        return CommonResult.success(commentService.createComment(commentDTO));
    }

    @Operation(summary = "获取帖子评论列表")
    @GetMapping("/post/{postId}")
    public CommonResult<List<CommentVO>> getPostComments(@PathVariable Long postId) {
        List<CommentVO> comments = commentService.getPostComments(postId);
        log.debug("Fetched comments: {}", comments);
        return CommonResult.success(comments);
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return CommonResult.success(null);
    }
} 